package com.flower.controller.buyer;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.flower.annotation.Log;
import com.flower.domain.vo.buyer.ProductDetailVO;
import com.flower.domain.dto.buyer.product.ProductHotDto;
import com.flower.domain.dto.buyer.product.ProductQueryDto;
import com.flower.domain.entity.Product;
import com.flower.service.ProductService;
import com.flower.utils.AjaxResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * 购买者端商品管理
 */
@Slf4j
@RestController
@RequestMapping("/buyer/product")
public class ProductController {

    @Autowired
    private ProductService productService;

    /**
     * 分页获取商品列表
     * @param queryDto
     * @return
     */
    @GetMapping("/list")
    public AjaxResult getProductList(ProductQueryDto queryDto) {
        Page<ProductDetailVO> page = productService.pageList(queryDto);
        return AjaxResult.success(page);
    }

    /**
     * 获取商品详情
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public AjaxResult getProductDetail(@PathVariable Integer id) {
        ProductDetailVO detail = productService.getProductDetail(id);
        return AjaxResult.success(detail);
    }

    /**
     * 获取热门商品列表
     * @return
     */
    @GetMapping("/hot")
    public AjaxResult getHotProductList() {
        List<ProductHotDto> productHotDtoList = productService.getHotProductList();
        return AjaxResult.success(productHotDtoList);
    }

    /**
     * 编辑商品信息
     * @param product
     * @return
     */
    @Log(name = "编辑商品信息")
    @PutMapping("/edit")
    public AjaxResult editProduct(@RequestBody Product product) {
        log.info("编辑商品信息, 参数: {}", product);
        // 验证商品所属店铺
        Product existProduct = productService.getById(product.getProductId());
        if (existProduct == null) {
            return AjaxResult.error("商品不存在");
        }
        
        // 保留原有的创建时间和点击量
        product.setCreateTime(existProduct.getCreateTime());
        product.setClickCount(existProduct.getClickCount());
        
        boolean result = productService.updateById(product);
        return AjaxResult.success(result);
    }

    /**
     * 添加商品
     * @param product
     * @return
     */
    @Log(name = "添加商品")
    @PostMapping("/add")
    public AjaxResult addProduct(@RequestBody Product product) {
        // 设置创建时间
        product.setCreateTime(LocalDateTime.now());
        boolean result = productService.save(product);
        return AjaxResult.success(result);
    }

    /**
     * 删除商品
     * @param id
     * @return
     */
    @Log(name = "删除商品")
    @DeleteMapping("/delete/{id}")
    public AjaxResult deleteProduct(@PathVariable Integer id) {
        boolean result = productService.removeById(id);
        return AjaxResult.success(result);
    }

    /**
     * 更新商品状态
     * @param product
     * @return
     */
    @Log(name = "更新商品状态")
    @PutMapping("/status")
    public AjaxResult updateProductStatus(@RequestBody Product product) {
        Product existProduct = productService.getById(product.getProductId());
        if (existProduct == null) {
            return AjaxResult.error("商品不存在");
        }
        existProduct.setStatus(product.getStatus());
        boolean result = productService.updateById(existProduct);
        return AjaxResult.success(result);
    }

    /**
     * 更新商品点击量
     * @param id
     * @return
     */
    @PutMapping("click/{id}")
    public AjaxResult updateClickCount(@PathVariable Integer id) {
        boolean result = productService.updateClickCountById(id);
        return AjaxResult.success(result);
    }
} 